/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.cicp.service;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.cache.CacheFactory;
import com.sinodata.bsm.cicp.cache.ProtocolParameterCache;
import com.sinodata.bsm.cicp.util.DBConnectionUtil;
import com.sinodata.bsm.cicp.util.pool.SSH2ConnPool;
import com.sinodata.bsm.cicp.util.pool.SSHConnPool;
import com.sinodata.bsm.cicp.util.pool.TelnetConnPool;
import com.sinodata.bsm.cicp.util.telnet.pool.EthzSSH2Pool;
import com.sinodata.bsm.cicp.util.telnet.pool.SSHPool;
import com.sinodata.bsm.cicp.util.telnet.pool.TelnetPool;
import com.sinodata.bsm.common.constants.ProtocolConstants;
import com.sinodata.bsm.common.vo.ProtocolParameter;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author tangli
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-7-7 下午2:40:31          tangli         1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class ProtocalParameterService {

    private final ProtocolParameterCache protocolCache = (ProtocolParameterCache) CacheFactory.getCache(ProtocolParameterCache.class);

    private static final Logger logger = Logger.getLogger(ProtocalParameterService.class);

    /**
     * 默认构造方法
     */
    public ProtocalParameterService() {

    }

    /**
     * 修改采集协议
     * 
     * @param type
     * @param obj
     */
    public void changedProtocol(String type, ProtocolParameter obj) {
        if ("A".equals(type)) {
            protocolCache.put(obj.getId(), obj);
        } else if ("U".equals(type)) {
            removeProtocolParameter(protocolCache.get(obj.getId()));
            protocolCache.put(obj.getId(), obj);
        } else if ("R".equals(type)) {
            removeProtocolParameter(obj);
            protocolCache.remove(obj.getId());
        }
    }

    /**
     * 修改和删除数据库配置时设置原配置项不可用，设置原连接不可用
     * 
     * @param obj
     */
    private void removeProtocolParameter(ProtocolParameter protocolParameter) {
        if (ProtocolConstants.TYPE_JDBC.equals(protocolParameter.getType())) {
            String db = protocolParameter.getInstType();
            String ip = protocolParameter.getIp();
            String svrName = protocolParameter.getInstName();
            DBConnectionUtil.getInstance().setConnectionEnable(db, ip, svrName, false);
        } else if (ProtocolConstants.TYPE_HOST.equals(protocolParameter.getType())) {
            String protocol = protocolParameter.getProtocol();
            if (ProtocolConstants.SSH2.equals(protocol)) {
                try {
                    //                    EthzSSH2Pool.getInstance().freeSSH2(protocolParameter.getIp());
                    SSH2ConnPool.getInstance().updateUser(protocolParameter.getIp());
                } catch (Exception e) {
                    logger.error("failed to free ssh2 conn by host");
                }
            } else if (ProtocolConstants.TELNET.equals(protocol)) {
                try {
                    //                    TelnetPool.getInstance().freeTelnet(protocolParameter.getIp());

                    TelnetConnPool.getInstance().updateUser(protocolParameter.getIp());
                } catch (Exception e) {
                    logger.error("failed to free telnet conn by host");
                }
            } else if (ProtocolConstants.SSH.equals(protocol)) {
                try {
                    //                    SSHPool.getInstance().freeTelnet(protocolParameter.getIp());
                    SSHConnPool.getInstance().updateUser(protocolParameter.getIp());
                } catch (Exception e) {
                    logger.error("failed to free ssh conn by host");
                }
            }
        }
    }

}